<?php

/**
 * player actions.
 *
 * @package    eveladder
 * @subpackage player
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class playerActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()	{
  }
	
  public function executeDetails()  {
    if (!$this->getRequest()->getParameter('player'))	{
    	$this->forward404() ;
    }
		
		// get last challenges
  	$c = new Criteria() ;
  	$criterion = $c->getNewCriterion(ChallengePeer::P1_ID, $this->getRequest()->getParameter('player')) ;
  	$criterion->addOr($c->getNewCriterion(ChallengePeer::P2_ID, $this->getRequest()->getParameter('player'))) ;
  	$c->add($criterion) ;
  	$c->add(ChallengePeer::IS_VALID, 1) ;
  	$c->addDescendingOrderByColumn(ChallengePeer::CREATED_AT) ;
  	$this->challenges = ChallengePeer::doSelectWithPlayers($c) ;
  	    
    // get player infos
    $c = new Criteria() ;
    $c->add(PlayerPeer::USER_ID, $this->getRequest()->getParameter('player')) ;
    $this->player = PlayerPeer::doSelectJoinsfGuardUser($c) ;
    
    $this->player = $this->player[0] ;
  }
	
	public function handleErrorSubscribe()	{
		$this->getEVEInfos() ;
		return sfView::SUCCESS ;
	}
	
	public function getEVEInfos()	{
		// get pilotname
	  $this->pilotname = $_SERVER['HTTP_EVE_CHARNAME'] ;
	  $this->allianceid = $_SERVER['HTTP_EVE_ALLIANCEID'] ;
	  $this->corpid = $_SERVER['HTTP_EVE_CORPID'] ;
	  $this->charid = $_SERVER['HTTP_EVE_CHARID'] ;
	}
	
  public function executeSubscribe()  {
		$this->getEVEInfos() ;

		if ($this->getRequest()->getMethod() == sfRequest::POST)	{
			$authorized = false ;

			// check if in the good alliance
			$allowed_alliances = sfConfig::get('app_allowed_alliances') ;
			if (count($allowed_alliances) > 0 && in_array($this->allianceid, $allowed_alliances))	{
				$authorized = true ;
			}
			// check if in the good corporation
			$allowed_corporations = sfConfig::get('app_allowed_corporations') ;
			if (count($allowed_corporations) > 0 && in_array($this->corpid, $allowed_corporations))	{
				$authorized = true ;
			}
			
			if (!$authorized)	{
				$this->getRequest()->setError('corporation', 'you are not authorized to join this ladder') ;
				return sfView::SUCCESS ;
			}
	
			// check if already exists in the DB
			$c = new Criteria() ;
			$c->add(sfGuardUserPeer::ID, $this->pilotname) ;
			$count = sfGuardUserPeer::doCount($c) ;
			if ($count > 0)	{
				$this->getRequest()->setError('user', 'you already subscribed') ;
				return sfView::SUCCESS ;
			}

			myUser::subscribe($this->pilotname, $this->getRequest()->getParameter('password'), $this->charid) ;
			
			return 'Ok' ;
		}
  }
}
